﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessYaxNode
// 文件功能描述：设备组管理数据访问层
//
// 
// 创建标识：2012-02-09 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DbAdapter.sqldbAdapter;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessYaxNode
    {
        /// <summary>
        /// 获取设备组信息
        /// </summary>
        /// <returns></returns>
        public DataSet SelYaxNodeInfo(int pagesize, int startindex, string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from (select ROW_NUMBER() OVER(ORDER BY NodeID ASC) rownumber,* " +
                               "from Yax_Node) B "
                             + "where B.rownumber > " + startrow.ToString() + " and B.rownumber < " + endrow.ToString() + strquery;
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 设备组数量
        /// </summary>
        /// <returns></returns>
        public int GetYaxNodeCount()
        {
            sqldb db = new sqldb();
            string query = "select count(1) from Yax_Node";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 删除设备组
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public int DeleteYaxNode(string id)
        {
            sqldb db = new sqldb();
            string query = "select count(*) from Yax_Machine where NodeID in(" + id + ")";
            int rowcount = Convert.ToInt32(db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query));
            if (rowcount > 0)
            {
                rowcount = 0;
            }
            else
            {
                query = "Delete from Yax_Node where NodeID = '" + id + "'";
                rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            }
            
            return rowcount;
        }

        /// <summary>
        /// 保存设备组(新增界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveYaxNode(object[] yax)
        {
            sqldb db = new sqldb();
            string query = "insert into Yax_Node(Nodename,Remark) Values('" +
                yax[0] + "','" + yax[1] + "')";
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 根据编号获取一条设备组信息
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public DataSet GetYaxNodeInfoOne(string id)
        {
            sqldb db = new sqldb();
            string query = "select * from Yax_Node where NodeID = '" + id + "'";
            return db.ExecuteDataset(GetConntionString.conntionstring(), CommandType.Text, query);
        }

        /// <summary>
        /// 保存设备组(修改界面下)
        /// </summary>
        /// <param name="supp"></param>
        /// <returns></returns>
        public int SaveYaxNodeEdit(object[] supp)
        {
            sqldb db = new sqldb();
            string query = "update Yax_Node set Nodename='" + supp[1] + "',Remark='" + supp[2] + "' where NodeID=" + supp[0];
            int rowcount = db.ExecuteNonQuery(GetConntionString.conntionstring(), CommandType.Text, query);
            return rowcount;
        }

        /// <summary>
        /// 获取设备组信息(不分页）
        /// </summary>
        /// <returns></returns>
        public DataSet SelYaxNodeInfo(string condition)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select * from Yax_Node where 1=1 " + condition;
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        public static string GetYaxNodeName(string yid)
        {
            sqldb db = new sqldb();
            string query = "select Nodename from Yax_Node where NodeID = "+yid.ToString();
            object yaxname = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return yaxname.ToString();
        }
    }
}
